package com.luguangtao.controller;

import com.luguangtao.filter.MyGateWayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @类名: com.luguangtao.controller
 * @作者: lgt
 * @创建时间: 2019-12-05 10:56
 * @描述:
 **/
@Configuration
public class TestConfig {

    @Bean("myRemoteAddrKeyResolver")
    public MyRemoteAddrKeyResolver getKey(){
        return new MyRemoteAddrKeyResolver();
    }

    //路由绑定过滤器
    @Bean
    public RouteLocator getRouteLocator(RouteLocatorBuilder routeLocatorBuilder){
        RouteLocatorBuilder.Builder route = routeLocatorBuilder.routes().route(predicateSpec -> predicateSpec
                .path("/api/client/**")
                .filters(f -> f.stripPrefix(2).filter(getMyGateWayFilter()))
                .uri("http://localhost:10001")
                .id("gateway3-2")
        );

        return route.build();
    }

    private GatewayFilter getMyGateWayFilter() {
        return new MyGateWayFilter();
    }


}
